//
//  TKAccountManager.m
//  BasketBallLeagues
//
//  Created by 小禹宝宝 on 2017/5/16.
//  Copyright © 2017年 kenshin. All rights reserved.
//

#import "TKAccountManager.h"
//#import "BLRegisterAndLoginController.h"
#import "BLlocalStore.h"

#import <Bugtags/Bugtags.h>

@implementation TKAccountManager

+ (instancetype)sharedManager
{
    static TKAccountManager *sharedManager;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedManager = [self new];
        sharedManager.accountInfo = [BLlocalStore readaccountInfo];
    });
    return sharedManager;
}

+ (NSString *)userID {
    return [TKAccountManager sharedManager].accountInfo.result.info.user_id;
}

+ (NSString *)userName {
    return [TKAccountManager sharedManager].accountInfo.result.info.name;
}


+ (BOOL)isLogin
{
    int userID = [[self userID] intValue];
    if (userID > 0) {
        return YES;
    }
    return NO;
}

+ (void)logout
{
    [self clearData];
}

+ (void)clear {
    [self clearData];
}

+ (void)loginWithParams:(NSDictionary *)params anddecodeClass:(Class)decodeClass completion:(void (^)(NSInteger,NSString *))completion {
    [BLApi POST:URL_Login_verifyCode parameters:params decodeClass:decodeClass completion:^(id responseObject, id responseDataOriginal, NSError *error) {
        if (!error) {
            TKAccountInfo *resp = responseObject;
            if (!resp.errcode) {
                //登录成功
                //获取个人信息
                [TKAccountManager sharedManager].accountInfo = resp;
                [TKAccountManager saveAccountInfo];
                    NSDictionary *paramss = @{@"user_id":resp.result.info.user_id};
                @weakify(self)
                [BLApi POST:URL_PersonalInfo parameters:paramss decodeClass:[TKAccountInfo class] completion:^(id responseObject, id responseDataOriginal, NSError *error) {
                    @strongify(self)
                    if (!error) {
                        TKAccountInfo *response = (TKAccountInfo *)responseObject;
                        if (!response.errcode) {
                            [self clearData];
                            [TKAccountManager sharedManager].accountInfo = response;
                            [TKAccountManager saveAccountInfo];
                            [[NSNotificationCenter defaultCenter] postNotificationName:kUDLoginSuccess object:nil];
                        } else {
                            [self ks_showToast:response.errmsg];
                        }
                    }
                }];

            }
            if (completion) {
                completion(resp.errcode,resp.errmsg);
            }
            
            //           [self ks_showToast:resp.msg];
            DDLogInfo(@"%@",resp.errmsg);
        } else {
            DDLogWarn(@"%@",error);
        }
        
    }];
}


+ (void)clearData
{
    [TKAccountManager sharedManager].accountInfo = nil;
    [BLlocalStore storeTKAccountInfo:[TKAccountManager sharedManager].accountInfo];
}

+ (void)saveAccountInfo {
    [BLlocalStore storeTKAccountInfo:[TKAccountManager sharedManager].accountInfo];
}


+ (void)requestUserInfoCompletion:(void(^)())completion
{
    NSDictionary *params = @{@"user_id":[TKAccountManager sharedManager].accountInfo.result.info.user_id};
    [BLApi POST:URL_PersonalInfo parameters:params decodeClass:[TKAccountInfo class] completion:^(id responseObject, id responseDataOriginal, NSError *error) {
        if (!error) {
            TKAccountInfo *resp = responseObject;
            if (!resp.errcode) {
                //更新会员数据
                [TKAccountManager sharedManager].accountInfo = resp;
                resp = [TKAccountManager sharedManager].accountInfo;
                [BLlocalStore storeTKAccountInfo:resp];
            }
            if (completion) {
                completion();
            }
            DDLogInfo(@"%@",resp.errmsg);
        } else {
            DDLogWarn(@"%@",error);
        }
    }];
}

- (void)registerUserInfoToBugtags {
    if (self.accountInfo) {
        [Bugtags setUserData:self.accountInfo.result.info.user_id forKey:@"userid"];
        [Bugtags setUserData:self.accountInfo.result.info.name forKey:@"username"];
    }
}

@end
